package com.atguigu.boot;

import ch.qos.logback.core.db.DBHelper;
import com.atguigu.boot.bean.Pet;
import com.atguigu.boot.bean.User;
import com.atguigu.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import java.util.Arrays;

/**
 * 主程序类
 * @SpringBootApplication: 这是一个SpringBoot应用
 */
// @SpringBootApplication
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.atguigu")
public class MainApplication {
    public static void main(String[] args) {
        // 1. 返回我们IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class,args);
        // 2. 查看容器里面的组件
        String[] names = run.getBeanDefinitionNames();
        Arrays.stream(names).forEach(System.out::println);


        // 3. 从容器中获取组件
        // Pet tom01 = run.getBean("tom", Pet.class);
        // Pet tom02 = run.getBean("tom", Pet.class);
        //
        // System.out.println("组件: "+ (tom01 == tom02));
        //
        // //4.@Configuration(proxyBeanMethods = true) 时,com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$76683a30@7f34a967
        // // @Configuration(proxyBeanMethods = false) 时,com.atguigu.boot.config.MyConfig@11ce2e22
        // MyConfig bean = run.getBean(MyConfig.class);
        // System.out.println(bean);
        //
        // //如果@Configuration(proxyBeanMethods = true),代理对象调用方法.SpringBoot总会检查这个组件是否在容器中有
        // // 保持组件单实例
        // // com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$76683a30@7f34a967
        // User user = bean.user01();
        // User user1 = bean.user01();
        // System.out.println(user == user1);
        //
        // User user01 = run.getBean("user01", User.class);
        // Pet pet02 = run.getBean("tom",Pet.class);
        // System.out.println(user01.getPet() == pet02); // true
        //
        // // 5. 获取组件
        // String[] beanNamesForType = run.getBeanNamesForType(User.class);
        // System.out.println("============");
        // Arrays.stream(beanNamesForType).forEach(System.out::println);
        //
        // DBHelper bean1 = run.getBean(DBHelper.class);
        // System.out.println(bean1);

        boolean tom = run.containsBean("tom");
        System.out.println("容器中tom组件: "+tom);

        boolean user01 = run.containsBean("user01");
        System.out.println("容器中user01组件:"+ user01);


        boolean haha = run.containsBean("haha");
        boolean hehe = run.containsBean("hehe");

    }
}


